package com.yk.anima.servlet;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yk.anima.dao.ProductsDogMapper;
import com.yk.anima.pojo.ProductsDog;
import com.yk.anima.response.CommonCode;
import com.yk.anima.response.QueryResponseResult;
import com.yk.anima.response.QueryResult;
import com.yk.anima.response.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author xiaokai
 * @create 2021-05-14 20:22:16
 * @desc...对狗业务管理
 */

@Service
public class ProductsDogServlet {

    @Autowired
    ProductsDogMapper productsDogMapper;

    public List<ProductsDog> findAll() {
        List<ProductsDog> list = productsDogMapper.findAll();
        if (list.size() == 0) {
            return null;
        }
        return list;
    }

    public ProductsDog findById(int id) {
        ProductsDog productsDog = productsDogMapper.selectByPrimaryKey(id);
        if (productsDog != null) {
            return productsDog;
        }
        return null;
    }

    public ResponseResult save(ProductsDog productsDog) {
        if (productsDog==null){
            productsDog=new ProductsDog();
        }
        productsDog.setpId(null);
        productsDog.setDogId(3);
        int number = productsDogMapper.insertSelective(productsDog);
        if (number > 0) {
            return new ResponseResult(CommonCode.SUCCESS);
        }
        return new ResponseResult(CommonCode.FAIL);
    }

    public ResponseResult deleteById(int id) {
        int number = productsDogMapper.deleteByPrimaryKey(id);
        if (number > 0) {
            return new ResponseResult(CommonCode.SUCCESS);
        }
        return new ResponseResult(CommonCode.FAIL);
    }

    public ResponseResult update(int id, ProductsDog productsDog) {

        ProductsDog dog = productsDogMapper.selectByPrimaryKey(id);
        if (dog == null) {
            return new ResponseResult(CommonCode.FINDALLANDNULL);
        }

        dog.setDogId(3);
        dog.setAge(productsDog.getAge());
        dog.setDescription(productsDog.getDescription());
        dog.setGender(productsDog.getGender());
        dog.setName(productsDog.getName());
        dog.setPrice(productsDog.getPrice());

        int number = productsDogMapper.updateByPrimaryKeySelective(dog);
        if (number > 0) {
            return new ResponseResult(CommonCode.SUCCESS);
        }
        return new ResponseResult(CommonCode.FAIL);
    }

    public QueryResponseResult findByPage(int page, int size, String name) {

        if (page <= 0) {
            page = 1;
        }
        if (size <= 0) {
            size = 2;
        }
        PageHelper.startPage(page, size);
        Page<ProductsDog> dogPage = productsDogMapper.findByPage(name);
        long total = dogPage.getTotal();
        List<ProductsDog> result = dogPage.getResult();
        QueryResult queryResult = new QueryResult();
        queryResult.setTotal(total);
        queryResult.setList(result);
        return new QueryResponseResult(CommonCode.SUCCESS, queryResult);
    }
}
